/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.beans;

import com.icesoft.faces.component.paneltabset.PanelTabSet;
import com.icesoft.faces.component.paneltabset.TabChangeEvent;
import com.logic.HostAdmin;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Alejandro Malaver
 */
@ManagedBean
public class AdminItemsBean {

    //propiedades para crear nuevos servicios
    private String name;
    private String status;
    private String start;
    private String stop;
    private String restart;
    //propiedades para crear nuevos archivos
    private String fileName;
    private String filePath;
    //propiedades para el despliegue del popup
    private static String popupTitle = "Agregar Servicio";
    private static boolean addingService = true;
    private static boolean addingFile = false;
    private static boolean editingItem = false;

    public void clearFields(ActionEvent e) {
        name = "";
        status = "";
        start = "";
        restart = "";
        stop = "";
        fileName = "";
        filePath = "";
    }

    public String getPopupTitle() {
        popupTitle = addingService ? "Agregar Servicio" : "Agregar Archivo";
        popupTitle = editingItem ? "Editar" : popupTitle;
        return popupTitle;
    }

    public void switchAddingRenders(TabChangeEvent e) {
        int index = ((PanelTabSet) e.getComponent()).getSelectedIndex();
        addingService=index==0?true:false;
        addingFile=!addingService;
    }

    public void addService(ActionEvent e) {
        if (!(name.equals("") && status.equals("") && start.equals("") && restart.equals("") && stop.equals(""))) {
            HostAdmin.getInstance().addItem(0, name, start, stop, restart, status);
        }
    }

    public void addFile() {
        if (!(fileName.equals("") && filePath.equals(""))) {
            HostAdmin.getInstance().addItem(1, fileName,filePath);
        }
    }

//    public void loadEditInfo(ActionEvent e) {
//        ArrayList<Service> selectedList = new ArrayList<Service>(DetailsBean.instance.getSelectedService().keySet());
//        Service service = selectedList.get(0);
//        name = service.getDaemonName();
//        status = service.getStartCommand();
//        start = service.getStartCommand();
//        restart = service.getRestartCommand();
//        stop = service.getStopCommand();
//        editingItem = true;
//    }

//    public void saveChanges(ActionEvent e) {
//        String host = HostList.instance.getSelectedHost().getHostName();
//        ArrayList<Service> selectedList = new ArrayList<Service>(DetailsBean.instance.getSelectedService().keySet());
//        String item = selectedList.get(0).getDaemonName();
//        System.out.println(XMLParser.getInstance().modifyItem(item));
//    }
//
//    public void removeService(ActionEvent e) {
//        String host = HostList.instance.getSelectedHost().getHostName();
//
//        ArrayList<Service> selectedList = new ArrayList<Service>(DetailsBean.instance.getSelectedService().keySet());
//        String item = selectedList.get(0).getDaemonName();
//        System.out.println(XMLParser.getInstance().removeItem("/ring/files/", host, 1, item));
//        HostList.instance.setHostList(HostList.loadList());
//    }

    public boolean isEditingItem() {
        return editingItem;
    }

    public boolean isAddingService() {
        return addingService;
    }

    public boolean isAddingFile() {
        return addingFile;
    }

    public String getName() {
        return name;
    }

    public String getStatus() {
        return status;
    }

    public String getStart() {
        return start;
    }

    public String getStop() {
        return stop;
    }

    public String getRestart() {
        return restart;
    }

    public String getFileName() {
        return fileName;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public void setStop(String stop) {
        this.stop = stop;
    }

    public void setRestart(String restart) {
        this.restart = restart;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public static void setPopupTitle(String popupTitle) {
        AdminItemsBean.popupTitle = popupTitle;
    }

    public static void setAddingService(boolean addingService) {
        AdminItemsBean.addingService = addingService;
    }

    public static void setAddingFile(boolean addingFile) {
        AdminItemsBean.addingFile = addingFile;
    }
}
